<?php
/**
 *	FILENAME: 		/server_app/account_balances.php
 *	DESCRIPTION:	This backend script produces a JSON response representing a listing of the transactions on a given Wildcard account.
 *	AUTHOR:			Casey Burkhardt 
 *	VERSION:		1.1.0
 *	LAST MODIFIED:	12/3/2010
 **/

require_once($_SERVER['DOCUMENT_ROOT'] . "/config/environment.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/libraries/functions.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/libraries/logging.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/libraries/wildcard.php");

enforceSSL();
setRequestParameters();

$studentID = $_PARAMETERS["studentID"];
$password = $_PARAMETERS["password"];
$accountID = $_PARAMETERS["accountID"];

if ((strlen($studentID) == 0) || (strlen($password) == 0)) {
    $response["response_type"] = "error";
    $response["error_desc"] = "Student ID or password not provided.";
    logWarning("No user data supplied");
    echo json_encode($response);
    die();
} else if (strlen($accountID) == 0) {
    $response["response_type"] = "error";
    $response["error_desc"] = "Account ID not provided.";
    logWarning("No user data supplied");
    echo json_encode($response);
    die();
} else {
    // Create a Wildcard Office session with the student ID and pin provided.
    $sessionData = getWildcardSession($studentID, $password);

    // Validate the session.
    if ($sessionData === FALSE) {
        $response["response_type"] = "error";
        $response["error_desc"] = "Invalid Wildcard Office login information provided.";
        logWarning("User failed Wildcard Office login");
        echo json_encode($response);
        die();
    } else {
        $transactionData = getTransactionDetails($sessionData, $accountID);
        // Validate the data result.
        if ($transactionData === FALSE) {
            $response["response_type"] = "error";
            $response["error_desc"] = "Unable to obtain transaction details.";
            logError("Unable to obtain Wildcard transaction data");
            echo json_encode($response);
            die();
        } else {
            // Encode and return the transaction data.
            $response["response_type"] = "success";
            $response["transaction_data"] = $transactionData;
            logAction("Successfully returned Wildcard transaction data");
            echo json_encode($response);
            die();
        }
    }
}
?>
